package com.wt.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.config.SecUtils;
import com.wt.admin.entity.WqunAccount;
import com.wt.admin.service.WqunAdminService;
import com.wt.commonutils.R;
import com.wt.email.MailService;
import com.wt.encryptionPass.RandomUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 测试邮件发送
 * @author qzz
 */
@RestController
@RequestMapping("/email")
public class SendMailController {

    @Autowired
    private MailService mailService;

    @Autowired
    private WqunAdminService wqunAdminService;

    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation("修改密码邮箱验证信息")
    @GetMapping("/updatePassCode")
    @PreAuthorize("hasAnyAuthority('email:updatePassCode')")
    public R updatePassCode(){
        return sendTextMail(SecUtils.getUserMess().getEmail());
    }

    @ApiOperation("密码修改验证验证码是否正确")
    @GetMapping("/codeIsCorrect/{code}")
    public R codeIsCorrect(@PathVariable("code") String code){
        String emailCode = (String) redisTemplate.opsForValue().get(SecUtils.getUserMess().getEmail());//获取验证码

        if(emailCode == null || emailCode.equals("")){
            return R.error().message("未发送验证码或验证码已过期，请重新发送");
        }
        if(!emailCode.equals(code)){
            return R.error().message("验证码错误，请重新输入");
        }
        return R.ok();
    }

    @ApiOperation("邮箱验证验证码是否正确")
    @GetMapping("/emailCodeIsCorrect/{email}/{code}")
    public R emailCodeIsCorrect(@PathVariable("email") String email,@PathVariable("code") String code){

        String emailCode = (String) redisTemplate.opsForValue().get(email);//获取验证码
        if(emailCode == null || emailCode.equals("")){
            return R.error().message("未发送验证码或验证码已过期，请重新发送");
        }
        if(!emailCode.equals(code)){
            return R.error().message("验证码错误，请重新输入");
        }
        return R.ok();
    }


    @ApiOperation("绑定邮箱验证信息")
    @GetMapping("/bindEmailCode/{email}")
    @PreAuthorize("hasAnyAuthority('email:bindEmailCode')")
    public R bindEmailCode(@PathVariable("email") String email){
        return sendTextMail(email);
    }


    @ApiOperation("邮箱验证信息")
    @GetMapping("/sendTextMail/{to}")
    public R sendTextMail(@PathVariable("to") String to){
        //生成验证码
        String emailCode = (String) redisTemplate.opsForValue().get(to);
        if (emailCode == null) {
            // redis不存在，生成验证码发送
            String code = RandomUtil.getFourBitRandom();
            redisTemplate.opsForValue().set(to, code, 1, TimeUnit.MINUTES);
            String subject = "围群博客邮箱验证";
            String text = "【围群博客】,您的验证码"+code +",该验证码一分钟内有效，请勿泄露于他人。如未知情，请忽略。";
            return mailService.sendTextMailMessage(to, subject, text);
        }else {
            // redis存在直接发送
            String subject = "围群博客邮箱验证";
            String text = "【围群博客】,您的验证码"+emailCode +",该验证码一分钟内有效，请勿泄露于他人。如未知情，请忽略。";
            return mailService.sendTextMailMessage(to, subject, text);
        }
    }
}

